package com.xy.gateway.request.refund;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.xy.gateway.cert.CertManager;
import com.xy.gateway.exception.GateWayException;
import com.xy.gateway.model.EpayGateSetting;
import com.xy.gateway.model.Respara;
import com.xy.gateway.request.RequestHandler;
import com.xy.gateway.request.pub.RequestURLHandlerFactory;

public abstract class RequestRefundHandlerAbstract implements RequestHandler {
	protected HttpServletRequest req;
	protected HttpServletResponse res;
	protected String sid;
	protected String paytype;

	public RequestRefundHandlerAbstract(HttpServletRequest req,
			HttpServletResponse res, String sid, String paytype) {
		this.req = req;
		this.res = res;
		this.sid = sid;
		this.paytype = paytype;
	}

	public void doRequest() throws GateWayException {
		Map<String, String> responseValueMap = sendRequest();
		processResponse(responseValueMap);
	}

	protected Map<String, String> sendRequest() throws GateWayException {
		Map<String, String> responseValueMap = null;
		try {
			URL url = new URL(getURL());
			HttpURLConnection httpUrl = (HttpURLConnection) url
					.openConnection();
			httpUrl.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
			httpUrl.connect();
			int status = httpUrl.getResponseCode();

			if (status != 200) {

			} else {
				InputStream in = httpUrl.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(
						in, "utf-8"));
				String str = null;
				StringBuffer sb = new StringBuffer();
				while ((str = br.readLine()) != null) {
					sb.append(str).append("\n");
				}
				List<Respara> resList = EpayGateSetting.getResParaByPK(sid,
						"1", paytype);
				responseValueMap = analyzeDoc(sb.toString(), resList);
			}

		} catch (Exception e) {
			throw new GateWayException(e);
		}
		return responseValueMap;
	}

	protected void processResponse(Map<String, String> responseValueMap) {

	}

	protected String getURL() {
		String url = RequestURLHandlerFactory.getInstance()
				.createRequestURLHandler(req).getURL();
		if (url.indexOf("?") == -1) {
			url = url + "?" + getRequestPara();
		}
		return url;
	}

	private Map<String, String> analyzeDoc(String bankResultValue,
			List<Respara> paraList) {
		Document doc = null;
		Map<String, String> m = new HashMap<String, String>();
		try {
			doc = DocumentHelper.parseText(bankResultValue);
		} catch (DocumentException e) {
		}
		if (!"".equals(bankResultValue)) {
			Element root = doc.getRootElement();
			for (Respara p : paraList) {
				m.put(p.getResparaname(), root.elementText(p.getResparaname()));
			}
		}
		return m;
	}

	protected abstract String getRequestPara();
}
